<template>
  <el-collapse-item name="5">
    <template #title>
      <h2>AFE</h2>
    </template>

    <div v-if="config?.server">
      <h3>AFE 参数 配置</h3>
      <el-alert
        title="非专业人士请勿修改, 除非你知道你在做什么"
        type="warning"
        show-icon
        :closable="false"
        style="letter-spacing: 2px"
      />
      <el-alert
        title="采样电阻负数是为了改变计算的电流方向"
        type="info"
        show-icon
        :closable="false"
        style="letter-spacing: 2px"
      />
      <!-- <el-alert
        :title="`充放电电流配置建议小于` + 200000 / config.server.set_config.uR + `A`"
        type="info"
        show-icon
        :closable="false"
        style="letter-spacing: 2px"
      /> -->
      <el-divider />

      <el-row align="middle" style="margin-top: 20px">
        <el-col :span="12">
          <b>电流采样电阻(用于界面运算)</b>
        </el-col>
        <el-col :span="10">
          <el-input-number
            v-model="config.server.set_config.uR"
            style="width: 100%"
            :step="1"
          />
        </el-col>
        <el-col :span="2">
          <b>uR</b>
        </el-col>
      </el-row>

      <el-row
        v-for="(item, index) in show_number_list"
        :key="'AFE_CONFIG_LIST2_' + index"
        align="middle"
        style="margin-top: 20px"
      >
        <el-col :span="12" v-if="item.label">
          <b>{{ item.label }}</b>
        </el-col>
        <el-col :span="10" v-if="item.key">
          <el-select-v2
            v-if="item?.options"
            v-model="config.server.set_config.afe[item.key]"
            :options="item.options"
            :placeholder="`请选择${item.label}`"
            size="large"
            style="width: 100%"
          />
          <el-input-number
            v-else
            v-model="config.server.set_config.afe[item.key]"
            style="width: 100%"
            :step="item.step || 1"
            :min="item.min || 0"
            :max="item.max || 65535"
          />
        </el-col>
        <el-col v-if="item.unit" :span="2">
          <b>{{ item.unit }}</b>
        </el-col>
        <el-divider v-if="item?.divider"></el-divider>
      </el-row>

      <el-row
        v-for="(item, index) in show_list"
        :key="'AFE_CONFIG_LIST_' + index"
        align="middle"
        style="margin-top: 10px"
      >
        <el-divider v-if="item?.divider"></el-divider>
        <el-col v-if="item?.label" :span="10">
          <b>{{ item.label }}</b>
        </el-col>
        <el-col v-if="item?.options" :span="13">
          <el-select-v2
            v-model="config.server.set_config.afe[item.key]"
            :options="item.options"
            :placeholder="`请选择${item.label}`"
            size="large"
            style="width: 100%"
          />
        </el-col>
      </el-row>

      <el-button
        type="primary"
        style="margin-top: 10px; width: 90%"
        :loading="server_api.ticks?.afe_update_config > 0"
        :disabled="!change"
        @click="server_api.afe_update_config(config.server.set_config)"
      >
        保存
      </el-button>
    </div>
  </el-collapse-item>
</template>
<script setup lang="ts">
import store from "~/utils/store";
import server_api from "~/utils/server_api";
const config = toRef(store);
const change = computed(() => {
  return (
    JSON.stringify(config.value.server.set_config) !=
    JSON.stringify(config.value.server.user_config)
  );
});

/* 轮询周期 */
const pt2str = ["连续模式", "1秒", "2秒", "4秒", "8秒", "16秒", "32秒", "64秒"];
const ptSelects = pt2str.map((v, i) => ({ label: v, value: i }));
/* 电流硬件保护检测倍率 */
const gc2str = ["1倍", "2倍"];
const gcSelects = gc2str.map((v, i) => ({ label: v, value: i }));
/* 过流保护延时 */
const occd2str = [
  "32毫秒",
  "80毫秒",
  "160毫秒",
  "320毫秒",
  "640毫秒",
  "1.28秒",
  "2.56秒",
  "5.12秒",
];
const occdSelects = occd2str.map((v, i) => ({ label: v, value: i }));
/* 短路保护延时 */
const scdd2str = ["400微秒", "500微秒", "900微秒", "1.6毫秒"];
const scddSelects = scdd2str.map((v, i) => ({ label: v, value: i }));
/* 过流保护阈值[mV] */
const occtValue = [
  [
    2.3,
    5.2,
    8.1,
    10.9,
    13.8,
    16.7,
    19.6,
    22.5,
    25.3,
    28.2,
    31.1,
    34.0,
    36.9,
    39.7,
    42.6,
    45.5,
  ],
  [
    4.4,
    10.1,
    15.8,
    21.5,
    27.2,
    32.9,
    38.6,
    44.3,
    50.0,
    55.7,
    61.4,
    67.1,
    72.8,
    78.6,
    84.3,
    90.0,
  ],
];
const occt2str = [occtValue[0].map((v) => v + "mV"), occtValue[1].map((v) => v + "mV")];
const occtSelects = computed(() => {
  return [
    occt2str[0].map((v, i) => ({
      label:
        v +
        ` (${(
          (occtValue[0][i] * 1000) /
          Math.abs(config.value.server.set_config.uR)
        ).toFixed(3)} A)`,
      value: i,
    })),
    occt2str[1].map((v, i) => ({
      label:
        v +
        ` (${(
          (occtValue[1][i] * 1000) /
          Math.abs(config.value.server.set_config.uR)
        ).toFixed(3)} A)`,
      value: i,
    })),
  ];
});
/* 短路保护阈值[mV] */
const scdtValue = [
  [
    9.7,
    15.4,
    21.1,
    16.8,
    32.6,
    38.3,
    44.0,
    49.7,
    55.4,
    61.2,
    66.9,
    72.6,
    78.3,
    84.0,
    89.8,
    95.5,
  ],
  [
    18.8,
    30.2,
    41.5,
    52.8,
    64.2,
    75.5,
    86.8,
    98.2,
    109.5,
    120.8,
    132.1,
    143.5,
    154.8,
    166.1,
    177.5,
    188.8,
  ],
];
const scdt2str = [scdtValue[0].map((v) => v + "mV"), scdtValue[1].map((v) => v + "mV")];
const scdtSelects = computed(() => {
  return [
    scdt2str[0].map((v, i) => ({
      label:
        v +
        ` (${(
          (scdtValue[0][i] * 1000) /
          Math.abs(config.value.server.set_config.uR)
        ).toFixed(3)} A)`,
      value: i,
    })),
    scdt2str[1].map((v, i) => ({
      label:
        v +
        ` (${(
          (scdtValue[1][i] * 1000) /
          Math.abs(config.value.server.set_config.uR)
        ).toFixed(3)} A)`,
      value: i,
    })),
  ];
});
const show_number_list = computed(() => {
  if (config.value.server.set_config.afe.ovt < 0)
    config.value.server.set_config.afe.ovt = 0;
  if (config.value.server.set_config.afe.ovt > 256)
    config.value.server.set_config.afe.ovt = 255;
  if (config.value.server.set_config.afe.uvt < 0)
    config.value.server.set_config.afe.uvt = 0;
  if (config.value.server.set_config.afe.uvt > 256)
    config.value.server.set_config.afe.uvt = 255;

  /* 单节电池过压列表 */
  const bat_ovt_list = Array.from({ length: config.value.server.set_config.afe.ovt }).map(
    (_, i) => {
      let value = (i * 0.006 + 3.199).toFixed(3);
      return { value: i, voltage: value, label: value.toString() };
    }
  );
  /* 单节电池欠压列表 */
  const bat_uvt_list = Array.from({
    length: 256 - config.value.server.set_config.afe.uvt - 1,
  }).map((_, i) => {
    i += config.value.server.set_config.afe.uvt + 1;
    let value = (i * 0.006 + 1.898).toFixed(3);
    return {
      value: i,
      voltage: value,
      label: value.toString(),
    };
  });
  return [
    {
      label: "充/放电过流保护恢复时间",
      key: "cccd",
      step: 1,
      min: 0,
      max: 65535,
      unit: "秒",
    },
    {
      label: "放电短路保护恢复时间",
      key: "cscd",
      step: 1,
      min: 0,
      max: 65535,
      unit: "秒",
      divider: true,
    },
    {
      label: "电压校准",
      key: "calib_v",
      step: 0.001,
      min: 0.5,
      max: 10.0,
      unit: "倍",
    },
    {
      label: "电流校准",
      key: "calib_c",
      step: 0.001,
      min: 0.5,
      max: 10.0,
      unit: "倍",
    },
    {
      label: "电流过滤",
      key: "calib_f",
      step: 1,
      min: 0,
      max: 100000,
      unit: "mA",
    },
    {
      label: "自耗电",
      key: "calib_p",
      step: 0.1,
      min: 0,
      max: 100000,
      unit: "W",
      divider: true,
    },
    {
      label: "电池过压保护恢复阈值",
      key: "covt",
      options: bat_ovt_list,
    },
    {
      label: "电池欠压保护恢复阈值",
      key: "cuvt",
      options: bat_uvt_list,
    },
  ];
});
const show_list = computed(() => {
  return [
    {
      label: "电流传感通道轮询周期",
      key: "cs_pt",
      options: ptSelects,
      divider: true,
    },
    {
      label: "电压传感通道轮询周期",
      key: "vs_pt",
      options: ptSelects,
    },
    {
      label: "电流检测倍率",
      key: "cp_gc",
      options: gcSelects,
      divider: true,
    },
    {
      label: "充电过流保护",
      key: "occt",
      options: occtSelects.value[config.value.server.set_config.afe.cp_gc],
      divider: true,
    },
    {
      label: "充电过流保护延时",
      key: "occd",
      options: occdSelects,
    },
    {
      label: "放电过流保护",
      key: "ocdt",
      options: occtSelects.value[config.value.server.set_config.afe.cp_gc],
      divider: true,
    },
    {
      label: "放电过流保护延时",
      key: "ocdd",
      options: occdSelects,
    },
    {
      label: "放电短路保护",
      key: "scdt",
      options: scdtSelects.value[config.value.server.set_config.afe.cp_gc],
      divider: true,
    },
    {
      label: "放电短路保护延时",
      key: "scdd",
      options: scddSelects,
    },
  ];
});
</script>
<style scoped></style>
